package cn.liit.weixin.api.basic;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

import cn.liit.weixin.api.basic.config.Config;
import cn.liit.weixin.core.basic.accesstoken.AccessTokenSupport;

/**
 * 功能 ：提供获取accessToken的接口
 * url:/accesstoken
 * 响应:{
 * 		"access_token":"ghOmQNNtoK8Jeb6Amhb7Jwtzw_cK1eMtRnOZY9xtDzWobMz_GO6sg7yY_NRh__B8H3mogNr6s_iBlBOfeslNN11FLol3YA_deqSBlbrKDe0",
 * 		"expires_date":1444279797432,
 * 		"expires_in":7200,
 * 		"isloaded":true
 * 		}
 * @author zhangboqi
 * 
 */
@WebServlet(urlPatterns={"/accesstoken"})
public class AccessTokenServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String privatekey = request.getParameter("privatekey");
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		if(privatekey != null && privatekey.equals(Config.privatekey)){
			String token = JSONObject.toJSONString(AccessTokenSupport.getAccessToken());
			out.print(token);
		} else{
			out.print("{errcode:1,errmsg:'密钥不正确'}");
		}
		out.flush();
		out.close();
	}

}
